package com.jhz.speedy.component.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.jhz.speedy.component.control.Constants;
import com.jhz.speedy.component.service.DownLoadService;
import com.jhz.speedy.logic.entity.DownLoadInfo;

import java.util.ArrayList;

/**
 * 下载进度广播接收器
 * Created by JunHeZ on 2017/7/23.
 */

public class DownLoadReceiver extends BroadcastReceiver{
    private static ArrayList<DownEventListeners> listenerses = new ArrayList<>();

    public DownLoadReceiver(){
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if(Constants.ACTION_DOWNLOAD_RECEIVER.equals(intent.getAction())){
            int event = intent.getIntExtra(DownLoadService.DOWNLOAD_EVENT, DownLoadService.EVENT_DOWNLOAD_START);
            DownLoadInfo downLoadInfo = (DownLoadInfo) intent.getSerializableExtra(DownLoadService.DOWNLOAD_INFO);
            onDownEvent(event, downLoadInfo);
        }
    }

    /**
     * @param downEventListeners    添加监听器
     */
    public static void addDownEventListeners(DownEventListeners downEventListeners){
        listenerses.add(downEventListeners);
    }

    /**
     * @param downEventListeners    移除该监听器
     */
    public static void removeDownEventListeners(DownEventListeners downEventListeners){
        listenerses.remove(downEventListeners);
    }

    /**
     * 下载事件
     * @param event 事件
     * @param downLoadInfo  下载信息
     */
    private void onDownEvent(int event, DownLoadInfo downLoadInfo){
        for(int i = 0; i < listenerses.size(); i++){
            listenerses.get(i).onDownEvent(event, downLoadInfo);
        }
    }

}

